#include "carStop.h"
#include <iostream>
std::string getTimeStr(time_t time){
    tm *m=localtime(&time);//时间格式转换
    char s[1024]={0};
    sprintf(s,"%d年%d月%d日 %d:%d:%d",m->tm_year+1900,m->tm_mon+1,m->tm_mday,m->tm_hour,m->tm_min,m->tm_sec);
    return std::string(s);
}

void carStop::comeCar()
{
    car *c=new car("");
    std::cout<<"请输入车牌：";
    std::cin>>c->name;
    c->arriveTime=time(nullptr);//取本地时间
    std::cout<<"车辆|"<<c->name<<"|在"<<getTimeStr(c->arriveTime)<<"到达停车场\n";
    if(stop.Size()==size){
        std::cout<<"停车场已满，"<<c->name<<"停在便道上位置"<<pavement.Size()+1<<std::endl;
        pavement.push(c);
    }
    else{
        std::cout<<"停车场未满，停入场内第"<<stop.Size()+1<<"个位置"<<std::endl;
        stop.push(c);
    }
}

void carStop::leaveCar()
{
    std::cout<<"请输入要离开的车辆：";
    std::string leaveCarName;
    std::cin>>leaveCarName;

    linkStack tmpStop;//临时栈
    while(stop.isEmpty()==false){
        car *c=(car*)*stop.getTop();
        stop.pop();
        if(c->name==leaveCarName){//找到离场车
        c->leaveTime=time(nullptr);
        double money =1.0*(c->leaveTime-c->arriveTime)/3600*200;
        std::cout<<"车辆|"<<c->name<<"|于"
                <<getTimeStr(c->leaveTime)<<"离开停车场，费用|"
                <<money<<"元"
                <<std::endl;
        delete c;
        break;
        }
        tmpStop.push(c);
    }
    while(tmpStop.isEmpty()==false){
        stop.push(*tmpStop.getTop());
        tmpStop.pop();
    }
    //停车场有车位 且便道上有车
    if(stop.Size()<size&&pavement.isEmpty()==false){
        car *c=(car*)*pavement.getFront();
        std::cout<<"便道上的车|"<<c->name<<"在"<<getTimeStr(c->arriveTime)<<"进场"<<std::endl;
        pavement.pop();
        stop.push(c);
    }
}

void carStop::show()
{
    linkStack tmpStop;
    while(stop.isEmpty()==false){
        car *c=(car*)*stop.getTop();
        stop.pop();
        tmpStop.push(c);

    }
    while(tmpStop.isEmpty()==false){
        car *c=(car*)*tmpStop.getTop();
        std::cout<<"车|"<<c->name<<"|"<<std::endl;
        stop.push(c);
        tmpStop.pop();
        
    }
    linkQueen tmpPavement;
    while(pavement.isEmpty()==false){
        tmpPavement.push(*pavement.getFront());
        pavement.pop();
    }
    while(tmpPavement.isEmpty()==false){
        car *c=(car*)*tmpPavement.getFront();
        std::cout<<"车|"<<c->name<<"|"<<std::endl;
        //tmpPavement.push(*pavement.getFront());
        pavement.push(c);
        tmpPavement.pop();
    }

}

carStop::~carStop()
{
    while(stop.isEmpty()==false){
        car *c=(car*)*stop.getTop();
        delete c;
        stop.pop();
    }
    while(pavement.isEmpty()==false){
        car *c=(car*)*pavement.getFront();
        delete c;
        pavement.pop();
    }
}
